package net.edisoft.scheduler;

import java.text.ParseException;
import java.util.Map;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.impl.StdSchedulerFactory;

public class QuartzSchedulerListener implements ServletContextListener {

	public void contextDestroyed(ServletContextEvent arg0) {
		//
	}

	public void contextInitialized(ServletContextEvent arg0) {

		SchedulerTask task = new SchedulerTask();

		// specify your scheduler task details
		JobDetail job = new JobDetail();
		job.setName("xml_parse");
		job.setJobClass(SchedulerJob.class);

		Map dataMap = job.getJobDataMap();
		dataMap.put("schedulerTask", task);

		try {
			// configure the scheduler time, run it every 5 seconds
			CronTrigger trigger = new CronTrigger();
			trigger.setName("runJob");
			trigger.setCronExpression("0/10 * * * * ?");

			// schedule it
			Scheduler schedulerDemo = new StdSchedulerFactory().getScheduler();
			schedulerDemo.start();
			schedulerDemo.scheduleJob(job, trigger);

		} catch (ParseException e) {
			e.printStackTrace();
		} catch (SchedulerException e) {
			e.printStackTrace();
		}

	}
}
